package javaseof10day.day9.am.demo4;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class MainFrame extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	JLabel numLa, nameLa;
	JTextField numTxt, nameTxt;
	JButton numFBt, nameFBt, addBt, delBt, upBt;
	JTable table;
	JScrollPane panel;

	public static void main(String[] args) {

		MainFrame main = new MainFrame();
		
		
		main.LoginUI();
	}

	public void LoginUI() {
		// JFrame DengLu = new JFrame("欢迎使用书籍管理系统");
		// 设置关闭窗口时的默认操作
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 设置窗口位置
		this.setLocation(200, 200);
		// 设置窗口大小
		this.setSize(400, 300);
		// 设置窗体为空
		this.setLayout(null);
		// 插入密码按键
		JLabel passLa = new JLabel("密  码");
		passLa.setSize(80, 60);
		passLa.setLocation(60, 70);
		// 插入用户名按键
		JLabel userLa = new JLabel("用户名");
		userLa.setSize(80, 60);
		userLa.setLocation(60, 20);
		// 添加密码和用户名
		this.add(userLa);
		this.add(passLa);
		// 设置文本域
		JTextField userText = new JTextField(30);
		JTextField passText = new JTextField(30);
		userText.setLocation(130, 30);
		userText.setSize(150, 30);
		passText.setLocation(130, 80);
		passText.setSize(150, 30);
		// 添加文本域
		this.add(userText);
		this.add(passText);
		// 添加登陆和退出按钮
		JButton sureBut = new JButton("确认");
		JButton returnBut = new JButton("返回");
		sureBut.setLocation(60, 170);
		returnBut.setLocation(240, 170);
		sureBut.setSize(60, 40);
		returnBut.setSize(60, 40);
		// 添加按钮
		this.add(sureBut);
		this.add(returnBut);
		// 显示顶级容器
		this.setVisible(true);
		returnBut.addActionListener(e -> {
			userText.setText("");
			passText.setText("");
		});
		sureBut.addActionListener(e -> {
			String user = userText.getText();
			String pass = passText.getText();
			if (user.equals("admin") && pass.equals("admin")) {
				contentUI();
			}
		});

	}

	public void  contentUI() {
		
		this.setSize(800, 600);
		this.setTitle("书籍管理系统");
		this.getContentPane().removeAll();
		this.setLayout(null);
		numLa = new JLabel("名称");
		numLa.setSize(60, 30);
		numLa.setLocation(30, 30);
		this.add(numLa);
		numTxt = new JTextField();
		numTxt.setSize(150, 30);
		numTxt.setLocation(90, 30);
		this.add(numTxt);
		numFBt = new JButton("名称查找");
		numFBt.setSize(90, 30);
		numFBt.setLocation(280, 30);
		numFBt.addActionListener(this);
		this.add(numFBt);

		nameLa = new JLabel("风格");
		nameLa.setSize(60, 30);
		nameLa.setLocation(410, 30);
		this.add(nameLa);
		nameTxt = new JTextField();
		nameTxt.setSize(150, 30);
		nameTxt.setLocation(470, 30);
		this.add(nameTxt);
		nameFBt = new JButton("风格查找");
		nameFBt.setSize(90, 30);
		nameFBt.setLocation(660, 30);
		nameFBt.addActionListener(this);
		this.add(nameFBt);

		addBt = new JButton("增加");
		addBt.setSize(60, 30);
		addBt.setLocation(30, 90);
		addBt.addActionListener(this);
		this.add(addBt);
		delBt = new JButton("删除");
		delBt.setSize(60, 30);
		delBt.setLocation(120, 90);
		delBt.addActionListener(this);
		this.add(delBt);
		upBt = new JButton("修改");
		upBt.setSize(60, 30);
		upBt.setLocation(210, 90);
		upBt.addActionListener(this);
		this.add(upBt);

		ActionDAO dao = new ActionDAO();
		ArrayList<Books> list = dao.findStuBySname("");
		initTable(list);
		this.setVisible(true);
		
		
	}
	
	
	@SuppressWarnings("null")
	public void initTable(ArrayList<Books> stus)// 初始化表格的方法
	{
		if ((stus != null) || (stus.size() != 0)) {
			if (panel != null) {
				this.remove(panel);
			}
			String[] columnNames = { "编号", "名称", "价格", "数量", "日期", "风格" };
			String[][] values = new String[stus.size()][6];
			for (int i = 0; i < stus.size(); i++) {
				Books stu = (Books) stus.get(i);
				values[i][0] = String.valueOf(stu.getId());
				values[i][1] = stu.getName();
				values[i][2] = String.valueOf(stu.getPrice());
				values[i][3] = String.valueOf(stu.getNum());
				values[i][4] = stu.getDates();
				values[i][5] = stu.getStyle();
			}
			table = new JTable(values, columnNames);
			panel = new JScrollPane(table);
			panel.setSize(750, 400);
			panel.setLocation(20, 150);
			this.add(panel);
		}

	}

	public void actionPerformed(ActionEvent e) {
		JButton bt = (JButton) e.getSource();
		if (bt.getText().equals("风格查找")) {
			ActionDAO dao = new ActionDAO();
			ArrayList<Books> list = dao.findStuBySname(nameTxt.getText().trim());
			initTable(list);
		} else {
			if (bt.getText().equals("名称查找")) {
				ActionDAO dao = new ActionDAO();
			
				String nameString =numTxt.getText().trim();
				
				System.out.println(nameString);
				
				 ArrayList<Books> list = dao.findStuBySno(nameString);
					
				
				 
				 if(list.size()>0) {
						
						initTable(list);
						
					}
				
			} else {
				if (bt.getText().equals("删除")) {
					if (table.getSelectedRow() == -1) {
						JOptionPane.showMessageDialog(this, "请选中要删除的书籍名称");
					} else {
						ActionDAO dao = new ActionDAO();
						dao.delStudent(table.getValueAt(table.getSelectedRow(), 1).toString());
						ArrayList<Books> list = dao.findStuBySname("");
						initTable(list);
					}
				} else {
					if (bt.getText().equals("修改")) {
						if (table.getSelectedRow() == -1) {
							JOptionPane.showMessageDialog(this, "请选中要修改的书籍名称");
						} else {
							int row = table.getSelectedRow();
							String id = table.getValueAt(row, 0).toString();
							String name = table.getValueAt(row, 1).toString();
							int price = Integer.parseInt(table.getValueAt(row, 2).toString());
							int num = Integer.parseInt(table.getValueAt(row, 3).toString());
							String dates = table.getValueAt(row, 4).toString();
							String style = table.getValueAt(row, 4).toString();
							Books stu = new Books(name, price, num, dates, style);
							 new AddOrUpdateFrame("修改", stu, this);
						}
					} else {
						 new AddOrUpdateFrame("增加", null, this);
					}
				}
			}
		}
	}
}
